package icedot.library.common.dialog;

import icedot.library.common.R;
import icedot.library.common.base.BaseActivity;
import icedot.library.common.base.BaseDlgFragment;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ProgressDlgFragment extends BaseDlgFragment
{
	private String _content;
	private boolean _showProgressbar;
	private TextView _textContent;
	private ProgressBar _progressbar;
	
	/**
	 * 显示对话框
	 * @param act  父activity
	 * @param content   内容
	 * @param showProgressBar  是否显示菊花
	 * @return
	 */
	public static ProgressDlgFragment showDlg(BaseActivity act,String content,boolean showProgressBar)
	{
		if(act == null)
			return null;
		return showDlg(act, content, showProgressBar,true);
	}
	
	/**
	 * 显示对话框
	 * @param act  父activity
	 * @param content  内容
	 * @param showProgressBar  是否显示菊花
	 * @param enableCancel   back键是否能退出对话框
	 * @return
	 */
	public static ProgressDlgFragment showDlg(BaseActivity act,String content,
			boolean showProgressBar,boolean enableCancel)
	{
		if(act == null)
			return null;
		ProgressDlgFragment dlg = new ProgressDlgFragment();
		dlg.setContent(content);
		dlg.showProgressBar(showProgressBar);
		dlg.setCancelable(enableCancel);
		dlg.show(act);
		return dlg;
	}

    public ProgressDlgFragment()
    {
    	_content = "";
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	super.onCreate(savedInstanceState);
    	setStyle(DialogFragment.STYLE_NO_TITLE, 0);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    		Bundle savedInstanceState) 
    {
    	//return super.onCreateView(inflater, container, savedInstanceState);
    	View v = inflater.inflate(R.layout.dlg_progress, container, false);
    	_textContent = (TextView)v.findViewById(R.id.dlgprogress_text_content);
    	_progressbar = (ProgressBar)v.findViewById(R.id.dlgprogress_progressbar);
    	if(_showProgressbar)
    	{
    		_progressbar.setVisibility(View.VISIBLE);
    	}
    	else
    	{
    		_progressbar.setVisibility(View.GONE);
    	}
    	_textContent.setText(_content);
    	return v;
    }
    
    @Override
    public void onSaveInstanceState(Bundle arg0)
    {
    	super.onSaveInstanceState(arg0);
    }
    
    @Override
    public void show(FragmentManager manager, String tag)
    {
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commitAllowingStateLoss();
    }
    public void setContent(String content)
    {
    	if(content == null)
    	{
    		_content = "";
    		return;
    	}
    	_content = content;
    	
    	if(_textContent != null)
    	{
    		_textContent.setText(_content);
    	}
    }
    
    public void showProgressBar(boolean show)
    {
    	_showProgressbar = show;
    	if(_progressbar != null)
    	{
    		if(_showProgressbar)
        	{
        		_progressbar.setVisibility(View.VISIBLE);
        	}
        	else
        	{
        		_progressbar.setVisibility(View.GONE);
        	}
    	}
    	
    }
    
    public String getContent()
    {
    	return _content;
    }
    public TextView getTextViewContent()
    {
    	return _textContent;
    }
    
    public void show(BaseActivity context)
    {
    	if(context == null)
    		return;
    	show(context.getSupportFragmentManager(),"progressDlg");
    }
}
